/*     */ package com.zimbra.cs.mailbox.calendar;
/*     */ 
/*     */ import com.google.common.collect.ImmutableList;
/*     */ import com.zimbra.common.calendar.Attach;
/*     */ import com.zimbra.common.calendar.ParsedDateTime;
/*     */ import com.zimbra.common.calendar.ParsedDuration;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZParameter;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import com.zimbra.soap.mail.type.AlarmInfo;
/*     */ import com.zimbra.soap.mail.type.AlarmTriggerInfo;
/*     */ import com.zimbra.soap.mail.type.DurationInfo;
/*     */ import java.text.ParseException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ 
/*     */ public class Alarm
/*     */ {
/*     */   private final Action mAction;
/*     */   private final TriggerType mTriggerType;
/*     */   private TriggerRelated mTriggerRelated;
/*     */   private ParsedDuration mTriggerRelative;
/*     */   private ParsedDateTime mTriggerAbsolute;
/*     */   private ParsedDuration mRepeatDuration;
/*     */   private int mRepeatCount;
/*     */   private final String mDescription;
/*     */   private final String mSummary;
/*     */   private final Attach mAttach;
/*     */   private final List<ZAttendee> mAttendees;
/*     */   private final List<ZCalendar.ZProperty> xProps;
/*     */   public static final String XWRALARMUID = "X-WR-ALARMUID";
/*     */   private static final String FN_ACTION = "ac";
/*     */   private static final String FN_TRIGGER_TYPE = "tt";
/*     */   private static final String FN_TRIGGER_RELATED = "trd";
/*     */   private static final String FN_TRIGGER_RELATIVE = "tr";
/*     */   private static final String FN_TRIGGER_ABSOLUTE = "ta";
/*     */   private static final String FN_REPEAT_DURATION = "rd";
/*     */   private static final String FN_REPEAT_COUNT = "rc";
/*     */   private static final String FN_DESCRIPTION = "ds";
/*     */   private static final String FN_SUMMARY = "su";
/*     */   private static final String FN_NUM_ATTENDEES = "numAt";
/*     */   private static final String FN_ATTENDEE = "at";
/*     */   private static final String FN_ATTACH = "attach";
/*     */   
/*     */   public static enum Action
/*     */   {
/*  53 */     DISPLAY,  AUDIO,  EMAIL,  PROCEDURE,  NONE, 
/*     */     
/*  55 */     X_YAHOO_CALENDAR_ACTION_IM,  X_YAHOO_CALENDAR_ACTION_MOBILE;
/*     */     
/*     */     private Action() {}
/*  58 */     public static Action lookup(String str) { if (str != null) {
/*     */         try {
/*  60 */           str = str.replace('-', '_').toUpperCase();
/*  61 */           return valueOf(str);
/*     */         } catch (IllegalArgumentException e) {}
/*     */       }
/*  64 */       return null;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  69 */     public String toString() { return super.toString().replace('_', '-'); }
/*     */   }
/*     */   
/*     */   public static enum TriggerType {
/*  73 */     RELATIVE,  ABSOLUTE;
/*     */     
/*     */     private TriggerType() {}
/*  76 */     public static TriggerType lookup(String str) { if (str != null) {
/*     */         try {
/*  78 */           str = str.replace('-', '_').toUpperCase();
/*  79 */           return valueOf(str);
/*     */         } catch (IllegalArgumentException e) {}
/*     */       }
/*  82 */       return null;
/*     */     }
/*     */   }
/*     */   
/*     */   public static enum TriggerRelated {
/*  87 */     START,  END;
/*     */     
/*     */     private TriggerRelated() {}
/*  90 */     public static TriggerRelated lookup(String str) { if (str != null) {
/*     */         try {
/*  92 */           str = str.replace('-', '_').toUpperCase();
/*  93 */           return valueOf(str);
/*     */         } catch (IllegalArgumentException e) {}
/*     */       }
/*  96 */       return null;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 130 */   public String getDescription() { return this.mDescription; }
/* 131 */   public int getRepeatCount() { return this.mRepeatCount; }
/* 132 */   public Action getAction() { return this.mAction; }
/* 133 */   public List<ZAttendee> getAttendees() { return this.mAttendees; }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Alarm(Action action, TriggerType triggerType, TriggerRelated related, ParsedDuration triggerRelative, ParsedDateTime triggerAbsolute, ParsedDuration repeatDuration, int repeatCount, String description, String summary, Attach attach, List<ZAttendee> attendees, List<ZCalendar.ZProperty> xProperties)
/*     */     throws ServiceException
/*     */   {
/* 142 */     if (action == null)
/* 143 */       throw ServiceException.INVALID_REQUEST("Missing ACTION in VALARM", null);
/* 144 */     this.mAction = action;
/* 145 */     this.mTriggerType = triggerType;
/* 146 */     if (TriggerType.ABSOLUTE.equals(triggerType)) {
/* 147 */       if (triggerAbsolute == null)
/* 148 */         throw ServiceException.INVALID_REQUEST("Missing absolute TRIGGER in VALARM", null);
/* 149 */       this.mTriggerAbsolute = triggerAbsolute;
/*     */     } else {
/* 151 */       if (triggerRelative == null)
/* 152 */         throw ServiceException.INVALID_REQUEST("Missing relative TRIGGER in VALARM", null);
/* 153 */       this.mTriggerRelated = related;
/* 154 */       this.mTriggerRelative = triggerRelative;
/*     */     }
/* 156 */     if (repeatDuration != null) {
/* 157 */       this.mRepeatDuration = repeatDuration;
/* 158 */       this.mRepeatCount = repeatCount;
/*     */     }
/* 160 */     this.mDescription = description;
/* 161 */     this.mSummary = summary;
/* 162 */     this.mAttach = attach;
/* 163 */     this.mAttendees = attendees;
/* 164 */     if (xProperties == null) {
/* 165 */       this.xProps = ImmutableList.of();
/*     */     } else {
/* 167 */       this.xProps = ImmutableList.copyOf(xProperties);
/*     */     }
/*     */   }
/*     */   
/*     */   public Alarm newCopy() {
/* 172 */     List<ZAttendee> attendees = null;
/* 173 */     if (this.mAttendees != null) {
/* 174 */       attendees = new ArrayList(this.mAttendees.size());
/* 175 */       for (ZAttendee at : this.mAttendees) {
/* 176 */         attendees.add(new ZAttendee(at));
/*     */       }
/*     */     }
/*     */     
/* 180 */     Alarm copy = null;
/*     */     try {
/* 182 */       copy = new Alarm(this.mAction, this.mTriggerType, this.mTriggerRelated, this.mTriggerRelative, this.mTriggerAbsolute, this.mRepeatDuration, this.mRepeatCount, this.mDescription, this.mSummary, this.mAttach, attendees, this.xProps);
/*     */     }
/*     */     catch (ServiceException e) {}
/*     */     
/*     */ 
/* 187 */     return copy;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 192 */     StringBuilder sb = new StringBuilder();
/* 193 */     sb.append("action=").append(this.mAction.toString());
/* 194 */     sb.append(", triggerType=").append(this.mTriggerType.toString());
/* 195 */     if (TriggerType.ABSOLUTE.equals(this.mTriggerType)) {
/* 196 */       sb.append(", triggerAbsolute=").append(this.mTriggerAbsolute != null ? this.mTriggerAbsolute.toString() : "<none>");
/*     */     }
/*     */     else {
/* 199 */       sb.append(", triggerRelated").append(this.mTriggerRelated != null ? this.mTriggerRelated.toString() : "<default>");
/*     */       
/* 201 */       sb.append(", triggerRelative=").append(this.mTriggerRelative != null ? this.mTriggerRelative.toString() : "<none>");
/*     */     }
/*     */     
/* 204 */     if (this.mRepeatDuration != null) {
/* 205 */       sb.append(", repeatDuration=").append(this.mRepeatDuration != null ? this.mRepeatDuration.toString() : "<none>");
/*     */       
/* 207 */       sb.append(", repeatCount=").append(this.mRepeatCount);
/*     */     } else {
/* 209 */       sb.append(", repeat=<none>");
/*     */     }
/* 211 */     sb.append(", summary=\"").append(this.mSummary).append("\"");
/* 212 */     sb.append(", desc=\"").append(this.mDescription).append("\"");
/* 213 */     if (this.mAttach != null)
/* 214 */       sb.append(", attach=").append(this.mAttach.toString());
/* 215 */     if (this.mAttendees != null) {
/* 216 */       sb.append(", attendees=[");
/* 217 */       boolean first = true;
/* 218 */       for (ZAttendee attendee : this.mAttendees) {
/* 219 */         if (!first) {
/* 220 */           sb.append(", ");
/*     */         } else
/* 222 */           first = false;
/* 223 */         sb.append("[").append(attendee.toString()).append("]");
/*     */       }
/* 225 */       sb.append("]");
/*     */     }
/* 227 */     for (ZCalendar.ZProperty xprop : this.xProps) {
/* 228 */       sb.append(", ").append(xprop.toString());
/*     */     }
/* 230 */     return sb.toString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private List<ZCalendar.ZProperty> xpropsWithoutXWRAlarmUID()
/*     */   {
/* 241 */     List<ZCalendar.ZProperty> xprops = this.xProps;
/* 242 */     ZCalendar.ZProperty prop = getXProperty("X-WR-ALARMUID");
/* 243 */     if (prop != null) {
/* 244 */       xprops = com.google.common.collect.Lists.newArrayList();
/* 245 */       xprops.addAll(this.xProps);
/* 246 */       xprops.remove(prop);
/*     */     }
/* 248 */     return ImmutableList.copyOf(xprops);
/*     */   }
/*     */   
/*     */   public AlarmInfo toJaxb()
/*     */   {
/* 253 */     List<ZCalendar.ZProperty> useXprops = this.xProps;
/* 254 */     Action action; if (((Action.AUDIO.equals(this.mAction)) || (Action.PROCEDURE.equals(this.mAction))) && (DebugConfig.calendarConvertNonDisplayAlarm))
/*     */     {
/*     */ 
/* 257 */       Action action = Action.DISPLAY;
/* 258 */       useXprops = xpropsWithoutXWRAlarmUID();
/*     */     } else {
/* 260 */       action = this.mAction;
/*     */     }
/* 262 */     AlarmInfo alarm = new AlarmInfo(action.toString());
/* 263 */     AlarmTriggerInfo trigger = new AlarmTriggerInfo();
/* 264 */     alarm.setTrigger(trigger);
/* 265 */     if (TriggerType.ABSOLUTE.equals(this.mTriggerType)) {
/* 266 */       trigger.setAbsolute(new com.zimbra.soap.mail.type.DateAttr(this.mTriggerAbsolute.getDateTimePartString(false)));
/*     */     }
/*     */     else {
/* 269 */       DurationInfo relative = new DurationInfo(this.mTriggerRelative);
/* 270 */       trigger.setRelative(relative);
/* 271 */       if (this.mTriggerRelated != null)
/* 272 */         relative.setRelated(this.mTriggerRelated.toString());
/*     */     }
/* 274 */     if (this.mRepeatDuration != null) {
/* 275 */       DurationInfo repeat = new DurationInfo(this.mRepeatDuration);
/* 276 */       alarm.setRepeat(repeat);
/* 277 */       repeat.setRepeatCount(Integer.valueOf(this.mRepeatCount));
/*     */     }
/* 279 */     if (!Action.AUDIO.equals(action)) {
/* 280 */       alarm.setDescription(this.mDescription);
/*     */     }
/* 282 */     if ((!Action.DISPLAY.equals(action)) && (this.mAttach != null))
/* 283 */       alarm.setAttach(new com.zimbra.soap.mail.type.CalendarAttach(this.mAttach));
/* 284 */     if ((Action.EMAIL.equals(this.mAction)) || (Action.X_YAHOO_CALENDAR_ACTION_IM.equals(this.mAction)) || (Action.X_YAHOO_CALENDAR_ACTION_MOBILE.equals(this.mAction)))
/*     */     {
/*     */ 
/* 287 */       alarm.setSummary(this.mSummary);
/* 288 */       if (this.mAttendees != null) {
/* 289 */         for (ZAttendee attendee : this.mAttendees) {
/* 290 */           alarm.addAttendee(attendee.toJaxb());
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 295 */     alarm.setXProps(com.zimbra.cs.service.mail.ToXML.jaxbXProps(useXprops.iterator()));
/* 296 */     return alarm;
/*     */   }
/*     */   
/*     */   public Element toXml(Element parent) {
/* 300 */     Element alarm = parent.addElement("alarm");
/*     */     
/* 302 */     List<ZCalendar.ZProperty> useXprops = this.xProps;
/* 303 */     Action action; if (((Action.AUDIO.equals(this.mAction)) || (Action.PROCEDURE.equals(this.mAction))) && (DebugConfig.calendarConvertNonDisplayAlarm)) {
/* 304 */       Action action = Action.DISPLAY;
/* 305 */       useXprops = xpropsWithoutXWRAlarmUID();
/*     */     } else {
/* 307 */       action = this.mAction; }
/* 308 */     alarm.addAttribute("action", action.toString());
/* 309 */     Element trigger = alarm.addElement("trigger");
/* 310 */     if (TriggerType.ABSOLUTE.equals(this.mTriggerType)) {
/* 311 */       Element absolute = trigger.addElement("abs");
/* 312 */       absolute.addAttribute("d", this.mTriggerAbsolute.getDateTimePartString(false));
/*     */     } else {
/* 314 */       Element relative = this.mTriggerRelative.toXml(trigger, "rel");
/* 315 */       if (this.mTriggerRelated != null)
/* 316 */         relative.addAttribute("related", this.mTriggerRelated.toString());
/*     */     }
/* 318 */     if (this.mRepeatDuration != null) {
/* 319 */       Element repeat = this.mRepeatDuration.toXml(alarm, "repeat");
/* 320 */       repeat.addAttribute("count", this.mRepeatCount);
/*     */     }
/* 322 */     if (!Action.AUDIO.equals(action)) {
/* 323 */       Element desc = alarm.addElement("desc");
/* 324 */       if (this.mDescription != null)
/* 325 */         desc.setText(this.mDescription);
/*     */     }
/* 327 */     if ((!Action.DISPLAY.equals(action)) && (this.mAttach != null))
/* 328 */       this.mAttach.toXml(alarm);
/* 329 */     if ((Action.EMAIL.equals(this.mAction)) || (Action.X_YAHOO_CALENDAR_ACTION_IM.equals(this.mAction)) || (Action.X_YAHOO_CALENDAR_ACTION_MOBILE.equals(this.mAction)))
/*     */     {
/*     */ 
/* 332 */       Element summary = alarm.addElement("summary");
/* 333 */       if (this.mSummary != null)
/* 334 */         summary.setText(this.mSummary);
/* 335 */       if (this.mAttendees != null) {
/* 336 */         for (ZAttendee attendee : this.mAttendees) {
/* 337 */           attendee.toXml(alarm);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 342 */     com.zimbra.cs.service.mail.ToXML.encodeXProps(alarm, useXprops.iterator());
/* 343 */     return alarm;
/*     */   }
/*     */   
/*     */   public static boolean actionAllowed(Action action) {
/* 347 */     if ((Action.PROCEDURE.equals(action)) && (!DebugConfig.calendarAllowProcedureAlarms)) {
/* 348 */       com.zimbra.common.util.ZimbraLog.calendar.warn("Action " + action.toString() + " is not allowed; ignoring alarm");
/* 349 */       return false;
/*     */     }
/* 351 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Alarm parse(Element alarmElem)
/*     */     throws ServiceException
/*     */   {
/* 362 */     Action action = Action.DISPLAY;
/* 363 */     TriggerType triggerType = TriggerType.RELATIVE;
/* 364 */     TriggerRelated triggerRelated = null;
/* 365 */     ParsedDuration triggerRelative = null;
/* 366 */     ParsedDateTime triggerAbsolute = null;
/* 367 */     ParsedDuration repeatDuration = null;
/* 368 */     int repeatCount = 0;
/* 369 */     String description = null;
/* 370 */     String summary = null;
/* 371 */     Attach attach = null;
/* 372 */     List<ZAttendee> attendees = null;
/*     */     
/*     */ 
/* 375 */     String val = alarmElem.getAttribute("action");
/* 376 */     action = Action.lookup(val);
/* 377 */     if (action == null) {
/* 378 */       throw ServiceException.INVALID_REQUEST("Invalid action value " + val, null);
/*     */     }
/* 380 */     if (!actionAllowed(action)) {
/* 381 */       return null;
/*     */     }
/* 383 */     Element triggerElem = alarmElem.getElement("trigger");
/* 384 */     Element triggerRelativeElem = triggerElem.getOptionalElement("rel");
/* 385 */     if (triggerRelativeElem != null) {
/* 386 */       triggerType = TriggerType.RELATIVE;
/* 387 */       String related = triggerRelativeElem.getAttribute("related", null);
/* 388 */       if (related != null) {
/* 389 */         triggerRelated = TriggerRelated.lookup(related);
/* 390 */         if (triggerRelated == null) {
/* 391 */           throw ServiceException.INVALID_REQUEST("Invalid related value " + val, null);
/*     */         }
/*     */       }
/* 394 */       triggerRelative = ParsedDuration.parse(triggerRelativeElem);
/*     */     } else {
/* 396 */       triggerType = TriggerType.ABSOLUTE;
/* 397 */       Element triggerAbsoluteElem = triggerElem.getOptionalElement("abs");
/* 398 */       if (triggerAbsoluteElem == null) {
/* 399 */         throw ServiceException.INVALID_REQUEST("<trigger> must have either <rel> or <abs> child element", null);
/*     */       }
/*     */       
/*     */ 
/* 403 */       String datetime = triggerAbsoluteElem.getAttribute("d");
/*     */       try {
/* 405 */         triggerAbsolute = ParsedDateTime.parseUtcOnly(datetime);
/*     */       } catch (ParseException e) {
/* 407 */         throw ServiceException.INVALID_REQUEST("Invalid absolute trigger value " + val, e);
/*     */       }
/*     */     }
/*     */     
/* 411 */     Element repeatElem = alarmElem.getOptionalElement("repeat");
/* 412 */     if (repeatElem != null) {
/* 413 */       repeatDuration = ParsedDuration.parse(repeatElem);
/* 414 */       repeatCount = (int)repeatElem.getAttributeLong("count", 0L);
/*     */     }
/*     */     
/* 417 */     Element descElem = alarmElem.getOptionalElement("desc");
/* 418 */     if (descElem != null) {
/* 419 */       description = descElem.getText();
/*     */     }
/*     */     
/* 422 */     Element summaryElem = alarmElem.getOptionalElement("summary");
/* 423 */     if (summaryElem != null) {
/* 424 */       summary = summaryElem.getText();
/*     */     }
/*     */     
/* 427 */     Element attachElem = alarmElem.getOptionalElement("attach");
/* 428 */     if (attachElem != null) {
/* 429 */       attach = Attach.parse(attachElem);
/*     */     }
/* 431 */     Iterator<Element> attendeesIter = alarmElem.elementIterator("at");
/* 432 */     while (attendeesIter.hasNext()) {
/* 433 */       ZAttendee at = ZAttendee.parse((Element)attendeesIter.next());
/* 434 */       if (attendees == null)
/* 435 */         attendees = new ArrayList();
/* 436 */       attendees.add(at);
/*     */     }
/*     */     
/* 439 */     Alarm alarm = new Alarm(action, triggerType, triggerRelated, triggerRelative, triggerAbsolute, repeatDuration, repeatCount, description, summary, attach, attendees, com.zimbra.cs.service.mail.CalendarUtils.parseXProps(alarmElem));
/*     */     
/*     */ 
/*     */ 
/* 443 */     return alarm;
/*     */   }
/*     */   
/*     */   public ZCalendar.ZComponent toZComponent() throws ServiceException {
/* 447 */     ZCalendar.ZComponent comp = new ZCalendar.ZComponent(ZCalendar.ICalTok.VALARM);
/*     */     
/* 449 */     ZCalendar.ZProperty action = new ZCalendar.ZProperty(ZCalendar.ICalTok.ACTION, this.mAction.toString());
/* 450 */     comp.addProperty(action);
/*     */     
/* 452 */     ZCalendar.ZProperty trigger = new ZCalendar.ZProperty(ZCalendar.ICalTok.TRIGGER);
/* 453 */     if (TriggerType.ABSOLUTE.equals(this.mTriggerType)) {
/* 454 */       ZCalendar.ZParameter vt = new ZCalendar.ZParameter(ZCalendar.ICalTok.VALUE, ZCalendar.ICalTok.DATE_TIME.toString());
/* 455 */       trigger.addParameter(vt);
/* 456 */       trigger.setValue(this.mTriggerAbsolute.getDateTimePartString(false));
/*     */     } else {
/* 458 */       if (this.mTriggerRelated != null) {
/* 459 */         ZCalendar.ZParameter related = new ZCalendar.ZParameter(ZCalendar.ICalTok.RELATED, this.mTriggerRelated.toString());
/* 460 */         trigger.addParameter(related);
/*     */       }
/* 462 */       trigger.setValue(this.mTriggerRelative.toString());
/*     */     }
/* 464 */     comp.addProperty(trigger);
/*     */     
/* 466 */     if (this.mRepeatDuration != null) {
/* 467 */       ZCalendar.ZProperty duration = new ZCalendar.ZProperty(ZCalendar.ICalTok.DURATION, this.mRepeatDuration.toString());
/* 468 */       comp.addProperty(duration);
/* 469 */       ZCalendar.ZProperty repeat = new ZCalendar.ZProperty(ZCalendar.ICalTok.REPEAT, this.mRepeatCount);
/* 470 */       comp.addProperty(repeat);
/*     */     }
/*     */     
/* 473 */     if (!Action.AUDIO.equals(this.mAction)) {
/* 474 */       String d = this.mDescription;
/*     */       
/* 476 */       if ((d == null) && (!Action.PROCEDURE.equals(this.mAction)))
/* 477 */         d = "Reminder";
/* 478 */       ZCalendar.ZProperty desc = new ZCalendar.ZProperty(ZCalendar.ICalTok.DESCRIPTION, d);
/* 479 */       comp.addProperty(desc);
/*     */     }
/*     */     
/* 482 */     if (this.mAttach != null) {
/* 483 */       comp.addProperty(this.mAttach.toZProperty());
/*     */     }
/* 485 */     if ((Action.EMAIL.equals(this.mAction)) || (Action.X_YAHOO_CALENDAR_ACTION_IM.equals(this.mAction)) || (Action.X_YAHOO_CALENDAR_ACTION_MOBILE.equals(this.mAction)))
/*     */     {
/*     */ 
/* 488 */       String s = this.mSummary;
/* 489 */       if (s == null)
/* 490 */         s = "Reminder";
/* 491 */       ZCalendar.ZProperty summary = new ZCalendar.ZProperty(ZCalendar.ICalTok.SUMMARY, s);
/* 492 */       comp.addProperty(summary);
/*     */       
/*     */ 
/* 495 */       if (this.mAttendees != null) {
/* 496 */         for (ZAttendee attendee : this.mAttendees) {
/* 497 */           comp.addProperty(attendee.toProperty());
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 503 */     for (ZCalendar.ZProperty xprop : this.xProps) {
/* 504 */       comp.addProperty(xprop);
/*     */     }
/*     */     
/* 507 */     return comp;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Alarm parse(ZCalendar.ZComponent comp)
/*     */     throws ServiceException
/*     */   {
/* 517 */     Action action = Action.DISPLAY;
/* 518 */     TriggerType triggerType = TriggerType.RELATIVE;
/* 519 */     TriggerRelated triggerRelated = null;
/* 520 */     ParsedDuration triggerRelative = null;
/* 521 */     ParsedDateTime triggerAbsolute = null;
/* 522 */     ParsedDuration repeatDuration = null;
/* 523 */     int repeatCount = 0;
/* 524 */     String description = null;
/* 525 */     String summary = null;
/* 526 */     Attach attach = null;
/* 527 */     List<ZAttendee> attendees = null;
/*     */     
/* 529 */     List<ZCalendar.ZProperty> xprops = new ArrayList();
/* 530 */     Iterator<ZCalendar.ZProperty> propIter = comp.getPropertyIterator();
/* 531 */     while (propIter.hasNext()) {
/* 532 */       ZCalendar.ZProperty prop = (ZCalendar.ZProperty)propIter.next();
/* 533 */       ZCalendar.ICalTok tok = prop.getToken();
/* 534 */       String val = prop.getValue();
/* 535 */       if (tok == null) {
/* 536 */         String name = prop.getName();
/* 537 */         if ((name.startsWith("X-")) || (name.startsWith("x-"))) {
/* 538 */           xprops.add(prop);
/*     */         }
/*     */       }
/*     */       else {
/* 542 */         switch (tok) {
/*     */         case ACTION: 
/* 544 */           if (val != null) {
/* 545 */             action = Action.lookup(val);
/* 546 */             if (action == null)
/* 547 */               throw ServiceException.INVALID_REQUEST("Invalid ACTION value " + val, null);
/* 548 */             if (!actionAllowed(action))
/* 549 */               return null;
/*     */           }
/*     */           break;
/*     */         case TRIGGER: 
/* 553 */           ZCalendar.ZParameter valueType = prop.getParameter(ZCalendar.ICalTok.VALUE);
/* 554 */           if (valueType != null) {
/* 555 */             String vt = valueType.getValue();
/* 556 */             if (ZCalendar.ICalTok.DATE_TIME.toString().equals(vt))
/* 557 */               triggerType = TriggerType.ABSOLUTE;
/*     */           }
/* 559 */           if (TriggerType.RELATIVE.equals(triggerType)) {
/* 560 */             ZCalendar.ZParameter related = prop.getParameter(ZCalendar.ICalTok.RELATED);
/* 561 */             if (related != null) {
/* 562 */               String rel = related.getValue();
/* 563 */               if (rel != null) {
/* 564 */                 triggerRelated = TriggerRelated.lookup(rel);
/* 565 */                 if (triggerRelated == null)
/* 566 */                   throw ServiceException.INVALID_REQUEST("Invalid RELATED value " + rel, null);
/*     */               }
/*     */             }
/* 569 */             triggerRelative = ParsedDuration.parse(val);
/*     */           } else {
/*     */             try {
/* 572 */               if (val != null)
/* 573 */                 triggerAbsolute = ParsedDateTime.parseUtcOnly(val);
/*     */             } catch (ParseException e) {
/* 575 */               throw ServiceException.INVALID_REQUEST("Invalid TRIGGER value " + val, e);
/*     */             }
/*     */           }
/*     */           break;
/*     */         case DURATION: 
/* 580 */           if (val != null)
/* 581 */             repeatDuration = ParsedDuration.parse(val);
/*     */           break;
/*     */         case REPEAT: 
/* 584 */           if (val != null) {
/*     */             try {
/* 586 */               repeatCount = Integer.parseInt(val);
/*     */             } catch (NumberFormatException e) {
/* 588 */               throw ServiceException.INVALID_REQUEST("Invalid REPEAT value " + val, e);
/*     */             }
/*     */           }
/*     */           break;
/*     */         case DESCRIPTION: 
/* 593 */           description = val;
/* 594 */           break;
/*     */         case SUMMARY: 
/* 596 */           summary = val;
/* 597 */           break;
/*     */         case ATTACH: 
/* 599 */           attach = Attach.parse(prop);
/* 600 */           break;
/*     */         case ATTENDEE: 
/* 602 */           ZAttendee attendee = new ZAttendee(prop);
/* 603 */           if (attendees == null)
/* 604 */             attendees = new ArrayList();
/* 605 */           attendees.add(attendee);
/*     */         }
/*     */         
/*     */       }
/*     */     }
/* 610 */     Alarm alarm = new Alarm(action, triggerType, triggerRelated, triggerRelative, triggerAbsolute, repeatDuration, repeatCount, description, summary, attach, attendees, xprops);
/*     */     
/*     */ 
/* 613 */     return alarm;
/*     */   }
/*     */   
/* 616 */   private Iterator<ZCalendar.ZProperty> xpropsIterator() { return this.xProps.iterator(); }
/*     */   
/*     */   private ZCalendar.ZProperty getXProperty(String xpropName) {
/* 619 */     for (ZCalendar.ZProperty prop : this.xProps) {
/* 620 */       if (prop.getName().equalsIgnoreCase(xpropName))
/* 621 */         return prop;
/*     */     }
/* 623 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Alarm fromSimpleReminder(int minBeforeStart)
/*     */     throws ServiceException
/*     */   {
/* 633 */     return new Alarm(Action.DISPLAY, TriggerType.RELATIVE, TriggerRelated.START, ParsedDuration.parse(true, 0, 0, 0, minBeforeStart, 0), null, null, 0, null, null, null, null, null);
/*     */   }
/*     */   
/*     */   public static Alarm fromSimpleTime(ParsedDateTime time) throws ServiceException
/*     */   {
/* 638 */     return new Alarm(Action.DISPLAY, TriggerType.ABSOLUTE, null, null, time, null, 0, null, null, null, null, null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static String abbrevAction(Action action)
/*     */   {
/*     */     String str;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 656 */     switch (action) {
/* 657 */     case DISPLAY:  str = "d"; break;
/* 658 */     case AUDIO:  str = "a"; break;
/* 659 */     case EMAIL:  str = "e"; break;
/* 660 */     case PROCEDURE:  str = "p"; break;
/* 661 */     case NONE:  str = "n"; break;
/* 662 */     default:  str = action.toString();
/*     */     }
/* 664 */     return str;
/*     */   }
/*     */   
/*     */   private static Action expandAction(String abbrev) {
/* 668 */     if ((abbrev == null) || (abbrev.length() == 0)) {
/* 669 */       return Action.DISPLAY;
/*     */     }
/* 671 */     char ch = abbrev.charAt(0);
/* 672 */     Action action; switch (ch) {
/* 673 */     case 'd':  action = Action.DISPLAY; break;
/* 674 */     case 'a':  action = Action.AUDIO; break;
/* 675 */     case 'e':  action = Action.EMAIL; break;
/* 676 */     case 'p':  action = Action.PROCEDURE; break;
/* 677 */     case 'n':  action = Action.NONE; break;
/*     */     default: 
/* 679 */       action = Action.lookup(abbrev);
/* 680 */       if (action == null)
/* 681 */         action = Action.DISPLAY;
/*     */       break; }
/* 683 */     return action;
/*     */   }
/*     */   
/*     */   private static String abbrevTriggerType(TriggerType tt) {
/* 687 */     if ((tt == null) || (TriggerType.RELATIVE.equals(tt))) {
/* 688 */       return "r";
/*     */     }
/* 690 */     return "a";
/*     */   }
/*     */   
/*     */   private static TriggerType expandTriggerType(String abbrev) {
/* 694 */     if ((abbrev == null) || (abbrev.length() == 0))
/* 695 */       return TriggerType.RELATIVE;
/* 696 */     char ch = abbrev.charAt(0);
/* 697 */     if (ch == 'a') {
/* 698 */       return TriggerType.ABSOLUTE;
/*     */     }
/* 700 */     return TriggerType.RELATIVE;
/*     */   }
/*     */   
/*     */   private static String abbrevTriggerRelated(TriggerRelated tr) {
/* 704 */     if (tr == null)
/* 705 */       return null;
/* 706 */     if (TriggerRelated.END.equals(tr)) {
/* 707 */       return "e";
/*     */     }
/* 709 */     return "s";
/*     */   }
/*     */   
/*     */   private static TriggerRelated expandTriggerRelated(String abbrev) {
/* 713 */     if ((abbrev == null) || (abbrev.length() == 0))
/* 714 */       return null;
/* 715 */     char ch = abbrev.charAt(0);
/* 716 */     if (ch == 'e') {
/* 717 */       return TriggerRelated.END;
/*     */     }
/* 719 */     return TriggerRelated.START;
/*     */   }
/*     */   
/*     */   public Metadata encodeMetadata() {
/* 723 */     Metadata meta = new Metadata();
/*     */     
/* 725 */     meta.put("ac", abbrevAction(this.mAction));
/* 726 */     meta.put("tt", abbrevTriggerType(this.mTriggerType));
/* 727 */     if (TriggerType.RELATIVE.equals(this.mTriggerType)) {
/* 728 */       meta.put("trd", abbrevTriggerRelated(this.mTriggerRelated));
/* 729 */       meta.put("tr", this.mTriggerRelative.toString());
/*     */     } else {
/* 731 */       meta.put("ta", this.mTriggerAbsolute.getDateTimePartString(false));
/*     */     }
/* 733 */     if (this.mRepeatDuration != null) {
/* 734 */       meta.put("rd", this.mRepeatDuration.toString());
/* 735 */       meta.put("rc", this.mRepeatCount);
/*     */     }
/* 737 */     meta.put("ds", this.mDescription);
/* 738 */     meta.put("su", this.mSummary);
/* 739 */     if (this.mAttach != null)
/* 740 */       meta.put("attach", Util.encodeMetadata(this.mAttach));
/* 741 */     if (this.mAttendees != null) {
/* 742 */       meta.put("numAt", this.mAttendees.size());
/* 743 */       int i = 0;
/* 744 */       for (Iterator<ZAttendee> iter = this.mAttendees.iterator(); iter.hasNext(); i++) {
/* 745 */         ZAttendee at = (ZAttendee)iter.next();
/* 746 */         meta.put("at" + i, at.encodeAsMetadata());
/*     */       }
/*     */     }
/*     */     
/* 750 */     if (this.xProps.size() > 0) {
/* 751 */       Util.encodeXPropsAsMetadata(meta, xpropsIterator());
/*     */     }
/* 753 */     return meta;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Alarm decodeMetadata(Metadata meta)
/*     */     throws ServiceException
/*     */   {
/* 764 */     Action action = expandAction(meta.get("ac"));
/* 765 */     if (!actionAllowed(action)) {
/* 766 */       return null;
/*     */     }
/* 768 */     TriggerType tt = expandTriggerType(meta.get("tt"));
/* 769 */     TriggerRelated triggerRelated = null;
/* 770 */     ParsedDuration triggerRelative = null;
/* 771 */     ParsedDateTime triggerAbsolute = null;
/* 772 */     if (TriggerType.ABSOLUTE.equals(tt)) {
/*     */       try {
/* 774 */         triggerAbsolute = ParsedDateTime.parseUtcOnly(meta.get("ta"));
/*     */       } catch (ParseException e) {
/* 776 */         throw ServiceException.FAILURE("Error parsing metadata for alarm", e);
/*     */       }
/*     */     } else {
/* 779 */       triggerRelative = ParsedDuration.parse(meta.get("tr"));
/* 780 */       triggerRelated = expandTriggerRelated(meta.get("trd", null));
/*     */     }
/* 782 */     ParsedDuration repeatDuration = null;
/* 783 */     int repeatCount = 0;
/* 784 */     String val = meta.get("rd", null);
/* 785 */     if (val != null) {
/* 786 */       repeatDuration = ParsedDuration.parse(val);
/* 787 */       repeatCount = (int)meta.getLong("rc", 0L);
/*     */     }
/* 789 */     String description = meta.get("ds", null);
/* 790 */     String summary = meta.get("su", null);
/*     */     
/* 792 */     Attach attach = null;
/* 793 */     Metadata metaAttach = meta.getMap("attach", true);
/* 794 */     if (metaAttach != null) {
/* 795 */       attach = Util.decodeAttachFromMetadata(metaAttach);
/*     */     }
/* 797 */     int numAts = (int)meta.getLong("numAt", 0L);
/* 798 */     List<ZAttendee> attendees = new ArrayList(numAts);
/* 799 */     for (int i = 0; i < numAts; i++) {
/*     */       try {
/* 801 */         Metadata metaAttendee = meta.getMap("at" + i, true);
/* 802 */         if (metaAttendee != null)
/* 803 */           attendees.add(new ZAttendee(metaAttendee));
/*     */       } catch (ServiceException e) {
/* 805 */         com.zimbra.common.util.ZimbraLog.calendar.warn("Problem decoding attendee " + i + " in ALARM ");
/*     */       }
/*     */     }
/*     */     
/* 809 */     Alarm alarm = new Alarm(action, tt, triggerRelated, triggerRelative, triggerAbsolute, repeatDuration, repeatCount, description, summary, attach, attendees, Util.decodeXPropsFromMetadata(meta));
/*     */     
/*     */ 
/*     */ 
/* 813 */     return alarm;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public long getTriggerTime(long instStart, long instEnd)
/*     */   {
/* 825 */     if (TriggerType.ABSOLUTE.equals(this.mTriggerType)) {
/* 826 */       assert (this.mTriggerAbsolute != null);
/* 827 */       return this.mTriggerAbsolute.getUtcTime();
/*     */     }
/* 829 */     if (TriggerRelated.END.equals(this.mTriggerRelated)) {
/* 830 */       return this.mTriggerRelative.addToTime(instEnd);
/*     */     }
/* 832 */     return this.mTriggerRelative.addToTime(instStart);
/*     */   }
/*     */   
/*     */   public ParsedDuration getTriggerRelative() {
/* 836 */     return this.mTriggerRelative;
/*     */   }
/*     */   
/*     */   public ParsedDateTime getTriggerAbsolute() {
/* 840 */     return this.mTriggerAbsolute;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/Alarm.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */